package sdk3.components.remoteService
{
	import mx.managers.SystemManager;
	
	import sdk3.c_MATH;

	public class user extends Object
	{
		/*******************************************************************************
		 * Software: user.as												
		 * Version:  1.1
		 * Date:     since 2010_12_21_TIME_09:03:19
		 * Author:
		 
		 * PURPOSE:
		 * defines a user 
		 * USAGE EXAMPLE:
		 * LICENSE:  GNU General Public License (http://www.gnu.org/licenses/gpl.html) 	 *  	 * English: 		This program is free software; 		you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 		either version 3 of the License, or (at your option) any later version.   		This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 		without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 		See the GNU General Public License for more details. 		You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.   		 * Excerpt English: 		 * Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), 		 * that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things   		Auf Deutsch: 		Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht, 		weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version. 		Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, 		sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. 		Details finden Sie in der GNU General Public License. 		Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. 		Falls nicht, siehe <http://www.gnu.org/licenses/>.   		 * Auszug Deutsch: 		 * Wenn wir von freier Software sprechen, so beziehen wir uns auf Freiheit, nicht auf den Preis. 		 * Unsere Allgemeinen Öffentlichen Lizenzen sind darauf angelegt, sicherzustellen, daß Sie die Freiheit haben, 		 * Kopien freier Software zu verbreiten (und dafür etwas zu berechnen, wenn Sie möchten), die Möglichkeit, 		 * daß Sie die Software als Quelltext erhalten oder den Quelltext auf Wunsch bekommen, daß Sie die Software 		 * ändern oder Teile davon in neuen freien Programmen verwenden dürfen und daß Sie wissen, daß Sie dies alles tun dürfen.   GNU General Public License (http://www.gnu.org/licenses/gpl.html)
		 
		 * English:
		 This program is free software;
		 you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation;
		 either version 3 of the License, or (at your option) any later version.
		 
		 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
		 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
		 See the GNU General Public License for more details.
		 You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
		 
		 * Excerpt English:
		 * Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish),
		 * that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things
		 
		 Auf Deutsch:
		 Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht,
		 weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
		 Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE,
		 sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK.
		 Details finden Sie in der GNU General Public License.
		 Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben.
		 Falls nicht, siehe <http://www.gnu.org/licenses/>.
		 
		 * Auszug Deutsch:
		 * Wenn wir von freier Software sprechen, so beziehen wir uns auf Freiheit, nicht auf den Preis.
		 * Unsere Allgemeinen Öffentlichen Lizenzen sind darauf angelegt, sicherzustellen, daß Sie die Freiheit haben,
		 * Kopien freier Software zu verbreiten (und dafür etwas zu berechnen, wenn Sie möchten), die Möglichkeit,
		 * daß Sie die Software als Quelltext erhalten oder den Quelltext auf Wunsch bekommen, daß Sie die Software
		 * ändern oder Teile davon in neuen freien Programmen verwenden dürfen und daß Sie wissen, daß Sie dies alles tun dürfen.
		 
		 * FEATURES:
		 
		 * TODO: XXX:
		 * CHANGELOG / CREDITS:
		 * WHO?							| WHAT?															| PRECIOUS TIME SPEND?	| WHY?
		 * 		| make properties of this object match table columns			| 1h					| otherwise register/insert user will fail.
		 **/

		/** class instances **/
		public var c_MATH_inst:c_MATH = new c_MATH();

		// about the user
		public var ID:String = '';
		public var username:String = '';
		public var password:String = '';
		public var email:String = '';
		public var postAddress:String = '';
		public var postalZipCode:String = '';
		public var street:String = '';
		public var place:String = '';
		public var country:String = '';
		public var gps:String = '';
		public var sex:String = ''; // male ♂ female ♀
		public var sexualOrientation:String = ''; // heterosexual homosexual bisexual lesbian
		public var age:String = '';
		public var misc1:String = '';
		public var misc2:String = '';
		public var misc3:String = '';
		
		// about other users
		// self description
		public var aboutMyself:String = '';

		public var logincount:int = 0;
		/** if the user has successfully authenticated @ the server **/
		public var loggedIn:Boolean = false;
		public var stage_width:Number = 0;
		public var stage_height:Number = 0;
		public var ARRAY_Direkthilfe:Array = new Array();
		
		public var OBJECT_creationComplete:Object = {grid_width:0,grid_height:0,grid_x:0,grid_y:0};

		/** wil be overwritten by a possible existing default config file
		 * /Volumes/DATA/JAVA/SETUP/ECLIPSE/OSX/32BIT/CARBON/eclipse-jee-galileo-SR2-macosx-carbon/Eclipse_JEE.app/Contents/MacOS/endpoint_xml.config
		 * where username + passwords can be stored for MySQL-Access more or less "safely" in an text-file on the server and not send back and forth between client & server.
		 * **/
		public var host:String = '';
		public var database:String = '';
		public var table:String = 'users'; // name of the table of the database where a list of all users is stored
		public var usernameColumn:String = 'username'; // name of the column where the username is stored
		public var passwordColumn:String = 'password'; // name of the column where the username is stored
		
		public var created:Number = new Date(0).getTime();
		public var deleted:Number = new Date(0).getTime();
		public var lastlogin:Number = new Date(0).getTime();

		public var guestbook:String = ""; // when visitors of a profile comment on the profile
		public var wishlist:String = ""; // the wishes of the user must be heard
		public var hopes:String = "";
		public var dreams:String = "";
		public var pathToFiles:String = ""; // where the files the user uploaded to the system will be stored

		public var groups:Array = new Array(); // groups of the user 

		public var chatTopics:String = ""; // topics that the user subscribed to: format: topic1,topic2,topic3

		public var messages:Array = new Array(); // messages for the user
		
		public var SYSTEMMANAGER:SystemManager = null;
		/** language code **/
		public var LANGUAGE:String = "EN";

		public var openWindows:String = ""; // used to save the "user-session", namely the name and position of the windows that the user last had open. format: window1Name=x:100,y:200,height:100,width:200;window2Name=x:100,y:200,height:100,width:200;

		public var selfdescription:String = "";
		public var status:String = "";
		public var enabled:Boolean = true;

		[Bindable]
		public var ping:Number = 0; // time of packets/requests from user to server and back, RequestReceivedTime - RequestSendTime
		public var pingRequestSendTime:Number = new Date(0).getTime(); // time when the request was send
		public var pingRequestReceivedTime:Number = 0; // time when the last request came back
		
		public var TOOLTIP:Object = {LAST_FOCUS_IN:0,LAST_X:0,LAST_Y:0,TEXT:""};
		
		/** 1. chatmessages
		* first it gets ALL messages created
		* then it gets ONLY messages of which the ID is greate, than the LAST transfered message,
		* thus only fetching NEW messages, when they arrive.
		 **/
		public var IDlastReceivedMessage:Number = 0; // ID of the last message received, get only messages with heigher ID, after first poll (getting all messages) (needs to be -1 to also retrive messages with ID of 0)

		/** if the password should be stored encrypted, mysql default is MD5 generate password hash: SELECT PASSWORD('djfkldjfkld!');
		 * possible values:
		 * clear text -> password is stored in clear text in database.
		 * md5		  -> password hash via mysql PASSWORD function.
		 * **/ 
		public var passwordEncryption:String = 'clear text';
		public function user(username:String = '',password:String = '',email:String = '',groups:Array = null,ID:String = '')
		{
			this.username = username;
			this.password = password;
			this.email = email;
			this.ID = ID;
			if(groups != null)
			{
				this.groups = groups;
			}
		}

		/** returns all the properties of a user as an object **/
		public function toObject():Object
		{
			// username 	password 	email 	created 	deleted 	lastlogin 	logincount 	guestbook 	selfdescription 	status 	wishlist 	hopes 	dreams 	pathToFiles 	enabled 	loggedIn 	openWindows 	ping
			
			var sex:String = "";
			if(this.sex == '♂')
			{
				sex = "male";
			}
			if(this.sex == '♀')
			{
				sex = "female";
			}
			
			return {
				ID:this.ID,
				username:this.username,
				password:this.password,
				created:this.created,
				email:this.email,
				deleted:this.deleted,
				lastlogin:this.lastlogin,
				logincount:this.logincount,
				guestbook:this.guestbook,
				wishlist:this.wishlist,
				selfdescription:this.selfdescription,
				status:this.status,
				hopes:this.hopes,
				dreams:this.dreams,
				age:this.age,
				sex:sex,
				sexualOrientation:this.sexualOrientation,
				pathToFiles:this.pathToFiles,
				enabled:c_MATH_inst.booleanToInt(this.enabled),
				loggedIn:c_MATH_inst.booleanToInt(this.loggedIn),
				openWindows:this.openWindows,
				IDlastReceivedMessage:this.IDlastReceivedMessage,
				ping:this.ping};
		}

		/** reads an object and copies all values that exist in the user object **/
		public function NewUserObject(OBJECT:Object):Object
		{
			var USER_OBJECT:Object = this.toObject();
			for(var KEY:String in USER_OBJECT)
			{
				if(OBJECT.hasOwnProperty(KEY))
				{
					if((KEY == "lastlogin") || (KEY == "deleted") || (KEY == "created"))
					{
						this[KEY] = OBJECT[KEY]; // new Date(OBJECT[KEY]);
					}
					else
					{
						this[KEY] = OBJECT[KEY];
					}
				}
			}
			
			return this;
		}

		/** list of all users, comes usually with every poll (check online status) **/
		private var _allUsers:Array = new Array();
		
		public function set allUsers(allUsers_temp:Array):void
		{
			this._allUsers = allUsers_temp;
		}
		public function get allUsers():Array
		{
			return this._allUsers;
		}
		
		/** list of all chatSessions **/
		private var _ChatSessions:Array = new Array();
		
		public function set ChatSessions(ChatSessions_temp:Array):void
		{
			this._ChatSessions = ChatSessions_temp;
		}
		public function get ChatSessions():Array
		{
			return this._ChatSessions;
		}
		
		/** finds the username by UserID **/
		public function USERID2USERNAME(USERID:String):String
		{
			var result:String = "";
			for each(var USER:Object in allUsers)
			{
				if(USER["ID"] == USERID)
				{
					result = USER["username"];
					break;
				}
			}
			
			return result;
		}
	}
}